From: | Andrew Markwell |
Date: | 28 Aug 99 at 17:42:45 |
Subject: | Re: ExecBase & 'RESET:starting PC' exception |
From: Andrew Markwell <andrewmarkwell@ukonline.co.uk>
On 28-Aug-99 did write:
> From: Ben Hutchings <womble@zzumbouk.demon.co.uk>
>
> On Fri, Aug 27, 1999 at 08:07:59PM -0600, Laura Vance wrote:
>> From: Laura Vance <vancel@amiga.nols.com>
>>
>> On Sat, 28 Aug 1999 01:30:57 +0100,
>> Ben Hutchings <womble@zzumbouk.demon.co.uk> wrote about Re: [amiga-c] Re:
>> ExecBase & 'RESET:starting PC' exception:
>>> From: Ben Hutchings <womble@zzumbouk.demon.co.uk>
>>>
>>> On Sat, Aug 28, 1999 at 12:44:33AM +0100, Andrew Markwell wrote:
>>>> From: Andrew Markwell <andrewmarkwell@ukonline.co.uk>
>>>>
>>>> On 27-Aug-99 did write:
>>>>> From: Ben Hutchings <womble@zzumbouk.demon.co.uk>
>>>>>
>>>>> On Fri, Aug 27, 1999 at 12:42:39AM +0100, Andrew Markwell wrote:
>>>>>> From: Andrew Markwell <andrewmarkwell@ukonline.co.uk>
>>>>>>
>>>>>> On 27-Aug-99 did write:
>>>>>> From: Ben Hutchings <womble@zzumbouk.demon.co.uk>
>>>
>>> <snip>
>>>>>>> You should use const char * here, not char *.
>>>>>>>
>>>>>> er, why?
>>>>>
>>>>> String literals are arrays of const char.
>>>>>
>>>> First I've heard of it.
>>>
>>> You doubt what I say? Perhaps you should find some decent documentation
>>> on standard C or C++.
There is no mention of such use of const in Herbert Schildt's, Ira Pohl's, Dave
Kelly's or Adrian Robson's (my own lecturer) books.
>>> For hysterical raisins, you are allowed to assign string literals to
>>> objects of type char *. That's just a special case; it doesn't mean that
>>> they are really arrays of char.
>>>
>>
>> I know that I'm sticking my nose in a conversation where it's not welcome,
>> but it seems to me that a char * is a pointer to the beginning of a null
>> terminated array of type char.
>
> char * is a pointer to char; it may point to just one char or to the
> first element of an array of char, which may or may not be null-
> terminated.
It is considered bad practice to use an array of char's for anything other than
a character array representing a string. If you really must, then use a short
int instead.
>> In the C and C++ classes that I've taken in college, they say to
>> reserve the const char * for something that will definately not
>> change.
>
> That's not quite right. You use pointers of type const char * where
> you do not need to modify the characters pointed to. The characters
> being pointed to might not really be constant, and may be modifiable
> from other parts of the program.
Laura is right. The C++ 'string' class is now the standard for using strings in
C++. And using 'const' in that fashion will only prevent the address to the
base of the character array from changing and not sequential characters. There
is in fact no way of preventing changes to such a data structure. If you wish a
string to be made constant then use the C++ string class with const.
>> And since we only saw a code fragment, we do not know that other
>> string literals will not be passed into the error handling function.
>
> Of course they may be, but that has no bearing on their type.
>
I could easily have written:
catch (string error)
{
}
instead. Then it doesn't matter whether you:
throw "error";
or
string x="error";
throw x;
or
char x[]="error";
throw x;